﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JXT.PrimaryKey.Batman.Cache;
using JXT.PrimaryKey.Batman.Container;
using JXT.PrimaryKey.Batman.Domain.Models;

namespace JXT.PrimaryKey.Batman.Domain.Services
{
    /// <summary>
    /// 提供缓存支持的服务基类
    /// </summary>
    /// <typeparam name="TEntity">模型类型</typeparam>
    public abstract class CachedServiceBase<TEntity> : ServiceBase<TEntity>, ICachedService where TEntity : IEntity
    {
        protected static readonly string CacheSpace = typeof(TEntity).Name; //基本命名空间名称

        private ICache _cache;

        /// <summary>
        /// 缓存处理对象
        /// </summary>
        public ICache Cache
        {
            get
            {
                if (_cache == null)
                {
                    _cache = ContainerManager.Default.Resolve<ICache>();
                }
                return _cache;
            }
        }

        /// <summary>
        /// 清除当前命名空间下的所有缓存
        /// </summary>
        public virtual void ClearCache()
        {
            Cache.Clear(CacheSpace);
        }
    }
}
